package com.irdstudio.efp.esb.service.impl.dxm;

import java.util.List;
import java.util.Objects;
import java.util.Optional;

import com.alibaba.fastjson.JSONObject;
import com.irdstudio.basic.framework.core.util.TraceUtil;
import com.irdstudio.efp.esb.common.client.resp.EsbRespRetInfBean;
import com.irdstudio.efp.esb.common.client.resp.EsbRespServiceBean;
import com.irdstudio.efp.esb.common.client.resp.EsbRespSysHeadBean;
import com.irdstudio.efp.esb.common.constant.dxm.reqbuilder.DXMBasicInfo;
import com.irdstudio.efp.esb.common.constant.dxm.reqbuilder.DXMReqBodyBuilder;
import com.irdstudio.efp.nls.service.vo.NlsApplyInfoVO;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.irdstudio.basic.framework.core.constant.ESBConstant;
import com.irdstudio.basic.framework.core.exception.ESBException;
import com.irdstudio.efp.esb.common.constant.dxm.DxmConstant;
import com.irdstudio.efp.esb.service.bo.req.dxm.ReqGetImageFileBean;
import com.irdstudio.efp.esb.service.bo.resp.dxm.RespGetImageFileBean;
import com.irdstudio.efp.esb.service.client.ESBClientFactory;
import com.irdstudio.efp.esb.service.client.impl.LhxESBBeanCreator;
import com.irdstudio.efp.esb.service.facade.dxm.GetImageFileService;

/**
 * 获取影像接口实现类: <br>
 * <p>
 * 0.1:caoxin:2019/3/25 10:33: 获取影像接口实现类: <br>
 * 0.2:caoxin:2019/3/25 10:33: 获取影像接口实现类修改: <br>
 *
 * @author caoxin
 * @version 0.2
 * @return
 * @date 2019/3/25 10:33
 * @since 2019/3/25 10:33
 */


@Service("getImageFileService")
public class GetImageFileServiceImpl implements GetImageFileService {

    /**
     * 日志
     */
    private static Logger logger = LoggerFactory.getLogger(GetImageFileServiceImpl.class);
    /**
     * 消息模板
     */
    private static final String msgTemplate = "message{ }";
    /**
     * 成功代码
     */
    private final String SUCCESS = "000000";
    /**
     * 获取ESB返回SYS_HEAD
     */
    private int RESULT_INDED = 0;

    /**
     * 获取影像文件接口: <br>
     * <p>
     * 0.1:caoxin:2019/3/25 10:23: <br>
     * 0.2:caoxin:2019/3/28 修改报文拼装
     *
     * session为用信流水号
     * reqSn为请求方需要保证永远唯一响应方据此判断是否重复请求
     * BdTransactionId事务ID，贯穿 授信、用信
     * @return
     * @author caoxin
     * @date 2019/3/25 10:23
     * @version 0.2
     * @since 2019/3/25 10:23
     */
    @Override
    public RespGetImageFileBean getImageFile(ReqGetImageFileBean reqGetImageFileBean, DXMBasicInfo basicInfo, NlsApplyInfoVO nlsApplyInfoVO) throws ESBException {
        RespGetImageFileBean respGetImageFileBean = null;
        String msg1 = "++++====>获取影像接口调用开始<====++++";
        logger.info(msg1, msgTemplate);
        // 构建请求实体
        TraceUtil.clear();
        DXMReqBodyBuilder.Builder requestBean = DXMReqBodyBuilder.ReqBuilder().
                buildAsyncRequest().
                buildRepeatRqsBss(nlsApplyInfoVO.getBdReqSn()).
                buildProdCd(nlsApplyInfoVO.getPrdCode()).
                buildMessageNecessityInfo(nlsApplyInfoVO.getApplySeq(), nlsApplyInfoVO.getBdTransactionId()).
                buildMessageEvTypeOfQuery().
                buildMessageBasicInfo(basicInfo).builderExpanding(reqGetImageFileBean).build();
        logger.info("++++====>获取影响照片的报文是为<====++++");
        logger.info(JSONObject.toJSONString(requestBean));

        // 发送请求
        String retCode = "";
        EsbRespServiceBean esbRespServiceBean = ESBClientFactory.
                buildClient().
                withTradeNo(DxmConstant.TRADE_NO_60120004).
                withScene(ESBConstant.SCENE_NO_01).
                withBody(requestBean).
                withESBBeanFactory(LhxESBBeanCreator.getINSTANCE()).
                create().
                sendAndReceive();
        // 获取请求
        respGetImageFileBean = esbRespServiceBean.getBody(RespGetImageFileBean.class);
        EsbRespSysHeadBean sysHead = esbRespServiceBean.getSysHead();

        // 处理结果 获取请求结果不为空 或 系统头不为空
        if (Objects.nonNull(sysHead)) {
            // 返回信息数据不为空
            List<EsbRespRetInfBean> retInfArry = sysHead.getRetInfArry();
            Objects.requireNonNull(retInfArry);
            // 返回信息数据第一个值不为空
            EsbRespRetInfBean esbRespRetInfBean = retInfArry.get(RESULT_INDED);
            Objects.requireNonNull(esbRespRetInfBean);

            retCode = Optional.ofNullable(esbRespRetInfBean.getRetCd()).orElse(StringUtils.EMPTY);
            if (SUCCESS.equals(retCode)) {
                String msg2 = "++++====>请求获取影像接口调用成功<====++++";
                logger.debug(msg2, msgTemplate);
            } else {
                String msg3 = "++++====>请求获取影像接口调用失败<====++++ " + esbRespRetInfBean.getRetMsg();
                logger.error(msg3);
                throw new ESBException(msg3);
            }
        }
        return respGetImageFileBean;
    }
}
